<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11-模拟物理运动1</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <script type="module">
    import * as THREE from "three";
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    import * as CANNON from "cannon";
    // 创建场景
    const scene = new THREE.Scene();
    // 设置背景颜色
    scene.background = new THREE.Color(0xbfd1e5);
    // 创建相机
    const camera = new THREE.PerspectiveCamera(
      45,
      window.innerWidth / window.innerHeight,
      1,
      1000
    );
    camera.position.set(0, 20, 40);
    camera.lookAt(0, 0, 0);
    // 创建渲染器 
    const renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    // 设置渲染器大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 把渲染的canvas内容添加到body
    document.body.appendChild(renderer.domElement);

    // 地面
    const plane = new THREE.Mesh(
      new THREE.PlaneGeometry(20, 20),
      new THREE.MeshBasicMaterial({
        color: 0x808080
      })
    );

    plane.rotation.x = -Math.PI / 2;
    scene.add(plane);

    // 小球
    const radius = 1;
    const textureLoader = new THREE.TextureLoader();
    const texture = textureLoader.load("../images/01.jpg");
    const sphere = new THREE.Mesh(
      new THREE.SphereGeometry(radius, 32, 32),
      new THREE.MeshBasicMaterial({
        map: texture
      })
    );
    sphere.position.set(-5, 5, 0);
    scene.add(sphere);

    // 创建物理世界
    const world = new CANNON.World();
    // 给y轴方向加重力，负值是向下的力，正数值是向上的力
    world.gravity.set(0, -9.82, 0);

    // 创建物理材料
    const groundMaterial = new CANNON.Material("groundMaterial");
    const sphereMaterial = new CANNON.Material("sphereMaterial"); 
    const contactMaterial = new CANNON.ContactMaterial(
      groundMaterial,
      sphereMaterial,
      {
        restitution: 0.8  // 反弹系数，0 - 1
      }
    );
    world.addContactMaterial(contactMaterial);

    // 创建物理地面
    const groundBody = new CANNON.Body({
      mass: 0,
      shape: new CANNON.Plane(),
      material: groundMaterial
    });
    groundBody.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
    world.addBody(groundBody);

    // 创建物理小球
    const sphereBody = new CANNON.Body({
      mass: 1,
      // position: new CANNON.Vec3(0, 3, 0),
      position: sphere.position,
      material: sphereMaterial,
      linearDamping: 0.5,
    })
    const sphereShape = new CANNON.Sphere(radius);
    sphereBody.addShape(sphereShape);
    sphereBody.applyLocalForce(
      new CANNON.Vec3(200, 0, 0),
      new CANNON.Vec3(0,-2, 0)
    );
    world.addBody(sphereBody);

    // 物理世界更新
    const updatePhysic = () => {
      world.step(1/60);
      sphere.position.copy(sphereBody.position);
      sphere.quaternion.copy(sphereBody.quaternion);
    }

    // 创建轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);

    // 开启物体的阻尼动画效果 
    controls.enableDamping = true;

    // 监听controls的change事件
    controls.addEventListener("change", () => {
      // 交互的鼠标每次发生变化，都需要渲染一次(如果用了animate函数就不需要).
      renderer.render(scene, camera);
    });
    renderer.render(scene, camera);


    function animate() {
      requestAnimationFrame(animate);
      controls.update();
      // 物理世界实时更新
      updatePhysic();
      renderer.render(scene, camera);
    } 

    // 添加动画并渲染
    animate();
  </script>
</body>

</html>